from research.calcor.pos import original_close
class signal_close(original_close):
    def __init__(self,key,name=""):
        self.key=key
        if name == "":
            self.name = type(self).__name__
        else:
            self.name = type(self).__name__ + "(" + str(name) + ")"
    def reset(self):
        pass
    def onbar(self,ss,b):
        if ss.pos=="buy":
            if b[self.key]==-2:
                order={"obj":b["obj"],"timekey":b["timekey"],"price":b["close"],"action": "closesell",
                     "info": self.name+"信号死叉平仓"+str(self.key)}
                ss.signals_data.append(order)
                ss.pos = None
                return True,"closesell",order["info"],self.open_price,self.open_price
            return False,"","",self.open_price,self.open_price
        elif ss.pos=="sell":
            if b[self.key] == 2:
                order = {"obj": b["obj"], "timekey": b["timekey"], "price": b["close"], "action": "closebuy",
                         "info": self.name+"信号金叉平仓" + str(self.key)}
                ss.signals_data.append(order)
                ss.pos = None
                return True, "closebuy",order["info"],self.open_price,self.open_price
            return False, "","", self.open_price, self.open_price
        return False,"","",None,None
